package it.canraid.leaflet.model;

import it.canraid.leaflet.control.Kernel;

import java.util.ArrayList;

public class LinePill {
    private static final String ID_ASCII = "L";
    private static final int N_ELEMENTS = 6;
    public float x1 = 0;
    public float y1 = 0;
    public float x2 = 0;
    public float y2 = 0;
    public int color = 0;
    public int size = 0;

    public LinePill(float x1, float y1, float x2, float y2) {
	super();
	this.x1 = x1;
	this.y1 = y1;
	this.x2 = x2;
	this.y2 = y2;
	this.color = Kernel.R().colorCurrent;
	this.size = Kernel.R().sizeCurrentDraw;
    }

    public LinePill(float x1, float y1, float x2, float y2, int color, int size) {
	this(x1, y1, x2, y2);
	this.color = color;
	this.size = size;
    }

    public LinePill clone() {
	return new LinePill(x1, y1, x2, y2, color, size);
    }

    public String generateAscii() {
	String response = "|" + ID_ASCII + ";" + x1 + ";" + y1 + ";" + x2 + ";" + y2 + ";" + color + ";" + size + ";";
	return response;
    }

    public static ArrayList<LinePill> extractLines(String ascii) {
	ArrayList<LinePill> response = new ArrayList<LinePill>();
	if (ascii == null)
	    return response;

	String[] asciiDecode = ascii.split(";");
	for (int q = 0; q < asciiDecode.length; q++) {
	    if (!asciiDecode[q].equals("|" + ID_ASCII))
		continue;
	    if (q + N_ELEMENTS >= asciiDecode.length)
		break;
	    try {
		float x1_ = Float.parseFloat(asciiDecode[++q]);
		float y1_ = Float.parseFloat(asciiDecode[++q]);
		float x2_ = Float.parseFloat(asciiDecode[++q]);
		float y2_ = Float.parseFloat(asciiDecode[++q]);
		int color_ = Integer.parseInt(asciiDecode[++q]);
		int size_ = Integer.parseInt(asciiDecode[++q]);
		response.add(new LinePill(x1_, y1_, x2_, y2_, color_, size_));
	    } catch (NumberFormatException e) {
		e.printStackTrace();
		continue;
	    }
	}
	return response;
    }
}
